package us.gaaoc.framework.util;

import us.gaaoc.framework.dao.ModelFacade;
import us.gaaoc.framework.model.Court;
import us.gaaoc.framework.model.LocalCaseCategory;
import us.gaaoc.framework.model.LocalCaseDivision;
import us.gaaoc.framework.model.LocalCaseFilingType;
import us.gaaoc.framework.model.LocalCaseType;
import us.gaaoc.framework.model.Organizations;

public class LocalCaseInformation 
{

	private LocalCaseDivision localCaseDivision;
	private LocalCaseType localCaseType;
	private LocalCaseCategory localCaseCategory;
	private LocalCaseFilingType localCaseFilingType;

	/**
	 * This function gets the court organization,
	 * not the OCSS organization!
	 * @param courtUuid The court
	 * @param mf The model facade
	 * @return The organization.
	 */
	public static Organizations getCourtOrganization(String courtUuid, ModelFacade mf)
	{
		Organizations organization;
		organization = mf.findOrganizationById(courtUuid); //Demo Court
		return organization;
	}
	
	/*
	 * TODO The division id's below are only valid for civil cases. This
	 * need to be updated to pull automatically based on case type.
	 */
	public static String getDivID(String courtUuid)
	{
		String divID = "12";
		if (courtUuid.equals("courtUuidBibb")) {
			divID = "12"; // for Bibb
		} else if (courtUuid.equals("courtUuid2")) {
			divID = "12"; // for Washington
		} else if (courtUuid.equals("courtUuid3")) {
			divID = "12"; // for Douglas 
		} else if (courtUuid.equals("courtUuidHart"))
		{
			divID = "12"; // for Hart
		}else if(courtUuid.equals("courtUuid4")) {
			divID = "12"; // for Fulton //TODO: verify the code
		}else if(courtUuid.equals("courtUuidJefferson")) {
			divID = "12"; // for Fulton //TODO: verify the code
		}else if(courtUuid.equals("courtUuid5")) {
			divID = "12"; // for Oglethorp //TODO: verify the code
		}else if(courtUuid.equals("courtUuidMacon")) {
			divID = "12"; // for Macon //TODO: verify the code
		}else if(courtUuid.equals("courtUuidChatham")) {
			divID = "12"; // for Chatham//TODO: verify the code		
		}else if(courtUuid.contains("fixture"))
		{
			divID = "99";
		}else{
			divID = "12";
		}
		return divID;
	}
	
	public static String up1(String s) {
		return (s.length()>0)? Character.toUpperCase(s.charAt(0))+s.substring(1) :
		s;
		}
	
	public static String upAllFirstWords(String s, String delimeter)
	{
		String ret = "";
		for(String x : s.split(delimeter))
		{
			ret += up1(x) + " ";
		}
		return ret.trim();
	}
	
	
	public static String getJurisdiction(String courtUuid) throws Exception
	{
		if(courtUuid.equals("courtUuidBibb")) {
			return "Bibb";
		}
		else if(courtUuid.equals("courtUuid2"))
		{	
			return "Washington";
		}else if(courtUuid.equals("courtUuid3")) 
		{
			return "Douglas";
		}else if(courtUuid.equals("courtUuidHart")) {
			return "Hart";
		} else if(courtUuid.equals("courtUuid4")){
			return "Fulton";
		}else if(courtUuid.equals("courtUuidJefferson")){
			return "Jefferson";
		}else if(courtUuid.equals("courtUuid5")){
			return "Oglethorpe";
		}else if(courtUuid.equals("courtUuidLee")){
			return "Lee";
		}else if(courtUuid.equals("courtUuidMacon")){
				return "Macon";
		}else if(courtUuid.equals("courtUuidChatham")){
			return "Chatham";
			
		}
		/* Houston County Added to list of Jurisdictions by Jogi*/
		else if(courtUuid.equals("courtUuidHouston"))
		{	
			return "Houston";
		}
		else if(courtUuid.equals("courtUuidClarke"))
		{
			return "Clarke";				
		}else{
			// Pulls jurisdiction from courtUuid, converting _ to a space and Camel Casing it.
			//  ben_hill -> Ben Hill
			return upAllFirstWords(courtUuid.substring("courtUuid".length(),courtUuid.length()).replace("_"," ")," ");									
		}
		//throw new Exception("NO JURISDICTION FOR COURT " + courtUuid);
	}
	
	public void populateLocalCaseInformation(Court c, ModelFacade mf) {
		if (c.getUuid().equals("courtUuidBibb")) {
			localCaseDivision = mf.findLocalCaseDivisionByCourtandCode(c, "12"); // for Bibb
			localCaseType = mf.findLocalCaseTypeByCourtandCode(c, "121110"); // for Bibb
		} else if (c.getUuid().equals("courtUuid2")) {
			localCaseDivision = mf.findLocalCaseDivisionByCourtandCode(c, "12"); // for Washington
			localCaseType = mf.findLocalCaseTypeByCourtandCode(c, "121110"); // for Washington
		} else if (c.getUuid().equals("courtUuid3")) {
			localCaseDivision = mf.findLocalCaseDivisionByCourtandCode(c, "12"); // for Douglas
			localCaseType = mf.findLocalCaseTypeByCourtandCode(c, "121110"); // for Douglas
		} else if (c.getUuid().equals("courtUuid4")) {
			localCaseDivision = mf.findLocalCaseDivisionByCourtandCode(c, "14"); // for Fulton
			localCaseType = mf.findLocalCaseTypeByCourtandCode(c, "121110"); // for Fulton
		} else if (c.getUuid().equals("courtUuidHart")) {
			localCaseDivision = mf.findLocalCaseDivisionByCourtandCode(c, "12"); // For Hart
			localCaseType = mf.findLocalCaseTypeByCourtandCode(c, "121110"); // For Hart
		} else if (c.getUuid().equalsIgnoreCase("courtUuidJefferson")) {
			localCaseDivision = mf.findLocalCaseDivisionByCourtandCode(c, "12"); // For Jefferson 
			localCaseType = mf.findLocalCaseTypeByCourtandCode(c, "121110"); // For Jefferson 
		} else if (c.getUuid().equalsIgnoreCase("courtUuid5")) {
			localCaseDivision = mf.findLocalCaseDivisionByCourtandCode(c, "12"); // For Jefferson 
			localCaseType = mf.findLocalCaseTypeByCourtandCode(c, "121110"); // For Jefferson 
		}else if (c.getUuid().equalsIgnoreCase("courtUuidMacon")) {
				localCaseDivision = mf.findLocalCaseDivisionByCourtandCode(c, "12"); // For Macon
				localCaseType = mf.findLocalCaseTypeByCourtandCode(c, "121110"); // For Macon 
		}else if (c.getUuid().equalsIgnoreCase("courtUuidChatham")) {
			localCaseDivision = mf.findLocalCaseDivisionByCourtandCode(c, "12"); // For Chatham	
			localCaseType = mf.findLocalCaseTypeByCourtandCode(c, "121110"); // For Chatham						
		}else if (c.getUuid().contains("fixture"))
		{
			localCaseDivision = mf.findLocalCaseDivisionByCourtandCode(c, "99"); // For Any other court" +					
			localCaseType = mf.findLocalCaseTypeByCourtandCode(c, "9991"); // For Any other court
		}else{
			localCaseDivision = mf.findLocalCaseDivisionByCourtandCode(c, "12"); // For Any other court" +					
			localCaseType = mf.findLocalCaseTypeByCourtandCode(c, "121110"); // For Any other court
		}

		
		if(c.getUuid().contains("fixture"))
		{
			localCaseCategory = mf.findLocalCaseCategoryByCourtandCode(c, "999");
			localCaseFilingType = mf.findLocalCaseFilingTypeByCourtandCode(c,"999");
		}else{		
			localCaseCategory = mf.findLocalCaseCategoryByCourtandCode(c, "121150");
			localCaseFilingType = mf.findLocalCaseFilingTypeByCourtandCode(c,"121110");
		}
	}
	
	public void populateFromExternalCaseCategoryCode(Court c, int externalCaseCategoryCode,
				ModelFacade mf)
	{
		String courtUuid = c.getUuid();
		String divID = getDivID(courtUuid);
		localCaseDivision = mf.findLocalCaseDivisionByCourtandCode(c, divID); 
		
		if (c.getUuid().equals("courtUuidCoweta")) 
		{
			switch(externalCaseCategoryCode)
			{
			case 1: 
				localCaseType = mf.findLocalCaseTypeByCourtandCode(c, "121110");
				//localCaseCategory = mf.findLocalCaseCategoryByCourtandCode(c, "121150");    Dom Rel: Support/URESA
				localCaseCategory = mf.findLocalCaseCategoryByCourtandCode(c, "121191");    // Dom Rel:  Separation/other
				localCaseFilingType = mf.findLocalCaseFilingTypeByCourtandCode(c, "121110");
				break;
			case 2: 
				localCaseType = mf.findLocalCaseTypeByCourtandCode(c, "121110");
				localCaseCategory = mf.findLocalCaseCategoryByCourtandCode(c, "121190");  //Dom Rel: Contempt
				localCaseFilingType = mf.findLocalCaseFilingTypeByCourtandCode(c, "121110");
				break;
			case 3: 
				localCaseType = mf.findLocalCaseTypeByCourtandCode(c, "121110");
				localCaseCategory = mf.findLocalCaseCategoryByCourtandCode(c, "121170");   //Dom Rel: Modification
				localCaseFilingType = mf.findLocalCaseFilingTypeByCourtandCode(c, "121110");
				break;
			case 4: 
				localCaseType = mf.findLocalCaseTypeByCourtandCode(c, "121110");
				localCaseCategory = mf.findLocalCaseCategoryByCourtandCode(c, "121191");
				localCaseFilingType = mf.findLocalCaseFilingTypeByCourtandCode(c, "121110");
				break;
			case 5: 
				localCaseType = mf.findLocalCaseTypeByCourtandCode(c, "121110");
				localCaseCategory = mf.findLocalCaseCategoryByCourtandCode(c, "121191");
				localCaseFilingType = mf.findLocalCaseFilingTypeByCourtandCode(c, "121110");
				break;
			case 6: 
				localCaseType = mf.findLocalCaseTypeByCourtandCode(c, "121110");
				localCaseCategory = mf.findLocalCaseCategoryByCourtandCode(c, "121150"); // Dom Rel: Support/UIFSA
				localCaseFilingType = mf.findLocalCaseFilingTypeByCourtandCode(c, "121110");
				break;
			default:
				localCaseType = mf.findLocalCaseTypeByCourtandCode(c, "121110");
				localCaseCategory = mf.findLocalCaseCategoryByCourtandCode(c, "121150"); // Dom Rel: Support/URESA
				localCaseFilingType = mf.findLocalCaseFilingTypeByCourtandCode(c, "121110");
				break;
			}
		}
		else
		{
			if(externalCaseCategoryCode == 2)
			{			
				localCaseType = mf.findLocalCaseTypeByCourtandCode(c, "121110");
				localCaseCategory = mf.findLocalCaseCategoryByCourtandCode(c, "121150");
				localCaseFilingType = mf.findLocalCaseFilingTypeByCourtandCode(c, "121110");
			}else
			{
				localCaseType = mf.findLocalCaseTypeByCourtandCode(c, "121110");	
				localCaseCategory = mf.findLocalCaseCategoryByCourtandCode(c, "121150");
				localCaseFilingType = mf.findLocalCaseFilingTypeByCourtandCode(c, "121110");
			}
		}
		
		
		if(courtUuid.contains("fixture"))
		{
			localCaseType = mf.findLocalCaseTypeByCourtandCode(c, "9991");
			localCaseCategory = mf.findLocalCaseCategoryByCourtandCode(c, "999");
			localCaseFilingType = mf.findLocalCaseFilingTypeByCourtandCode(c, "999");
		}
		
		
	}

	public LocalCaseDivision getLocalCaseDivision() {
		return localCaseDivision;
	}

	public LocalCaseType getLocalCaseType() {
		return localCaseType;
	}

	public LocalCaseCategory getLocalCaseCategory() {
		return localCaseCategory;
	}

	public LocalCaseFilingType getLocalCaseFilingType() {
		return localCaseFilingType;
	}
}
